package org.blog.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.blog.dao.BanIPDao;
import org.blog.model.ModelBanIP;
import org.springframework.beans.factory.annotation.Autowired;

public class BlogCacheMap implements BlogCache {

	@Autowired
	private BanIPDao banIPDao;

	private Map<String, Object> cache = new HashMap<String, Object>();

	private final String banIPKey = "blog_register_banip";

	@SuppressWarnings("unchecked")
	@Override
	public  Map<String, String> getRegisterBanIp() {
		if (KeyChange.get(KeyChange.banIP) == KeyChange.KEY_UNCHANGE) {
			return (Map<String, String>) cache.get(banIPKey);
		} else {
			return getBanIPFromDB();
		}
	}
	
	private synchronized Map<String, String> getBanIPFromDB(){
		ModelBanIP banIP=new ModelBanIP();
		banIP.setBanType(1);
		banIP.setCheck(1);
		List<ModelBanIP> list=banIPDao.getBanIPSelectList(banIP);
		Map<String, String> map=new HashMap<String, String>();
		if(list!=null){
			for(ModelBanIP t:list){
				map.put(t.getIp(), "1");
			}
		}
		KeyChange.put(KeyChange.banIP, KeyChange.KEY_UNCHANGE);
		return map;
	}



}
